<template>
    <!-- 能力组件 -->
    <section
        :style="{
            paddingTop: itemConfig.paddingTop + 'px',
            paddingBottom: itemConfig.paddingBottom + 'px',
            marginTop: itemConfig.marginTop + 'px',
            marginBottom: itemConfig.marginBottom + 'px',
            background: `linear-gradient(
                ${itemConfig.bgEdg}deg,
                ${itemConfig.bgColorOne},
                ${itemConfig.bgColorTow},
                ${itemConfig.bgColorThree})`
        }"
    >
        <div class="max-w-[1200px] m-auto px-5">
            <div
                v-if="itemConfig.title || itemConfig.intro"
                class="flex flex-col gap-1.5"
                :style="{ marginBottom: itemConfig.headerBottom + 'px' }"
            >
                <h3
                    v-if="itemConfig.title"
                    class="text-center text-7xl font-bold"
                    :style="{ color: itemConfig.titleColor }"
                >{{ itemConfig.title }}</h3>

                <p
                    v-if="itemConfig.intro"
                    class="text-center text-lg"
                    :style="{ color: itemConfig.introColor }"
                >{{ itemConfig.intro }}</p>
            </div>

            <div class="flex flex-col gap-6 relative">
                <template v-for="(item, index) in (itemData as itemDataType[])">
                    <div
                        v-if="item.isEnable"
                        :key="index"
                        class="p-6 flex items-center justify-between bg-page rounded-xl"
                        :style="{ background: item.bgColor }"
                    >
                        <!-- 左 -->
                        <div class="flex flex-col w-1/3 ml-5 mr-4">
                            <div
                                v-if="item.icon"
                                class="mb-5 w-14 h-14"
                            >
                                <el-image
                                    :src="appStore.toAbsoluteUrl(item.icon)"
                                    class="w-full h-full rounded"
                                />
                            </div>

                            <h2
                                v-if="item.name"
                                class="mb-2 text-5xl font-bold"
                                :style="{ color: item.nameColor }"
                            >
                                {{ item.name }}
                            </h2>

                            <p
                                v-if="item.desc"
                                class="text-base"
                                :style="{ color: item.descColor }"
                            >
                                {{ item.desc }}
                            </p>

                            <nuxt-link
                                class="block mt-10 text-baseÎ cursor-pointer hover:opacity-80"
                                :style="{ color: item.moreColor }"
                                @click="handleJump(item)"
                            >
                                {{ item.more }}
                            </nuxt-link>
                        </div>

                        <!-- 右 -->
                        <div class="flex justify-center w-3/5 min-h-[230px] max-h-[420px] rounded-lg">
                            <el-image
                                v-if="item.img"
                                :src="appStore.toAbsoluteUrl(item.img)"
                                :alt="item.name"
                                :lazy="true"
                            />
                        </div>
                    </div>
                </template>
            </div>
        </div>
    </section>
</template>

<script setup lang="ts">
import { popupEnum } from '~/enums/app'
import useAppStore from '~/stores/app'
import useUserStore from '~/stores/user'
import validateUtil from '~/utils/validate'

interface itemConfigType {
    title: string;
    intro: string;
    paddingTop: number;
    paddingBottom: number;
    marginTop: number;
    marginBottom: number;
    headerBottom: number;
    bgEdg: number;
    bgColorOne: string;
    bgColorTow: string;
    bgColorThree: string;
    titleColor: string;
    introColor: string;
}

interface itemDataType {
    img: string;
    icon: string;
    name: string;
    desc: string;
    more: string;
    nameColor: string;
    descColor: string;
    moreColor: string;
    bgColor: string;
    isLogin: boolean;
    isEnable: boolean;
    link: any;
}

withDefaults(
    defineProps<{
        itemConfig?: itemConfigType,
        itemData?: itemDataType[]
    }>(),
    {
        itemConfig: {} as itemConfigType | any,
        itemData: [] as itemDataType[] | any
    }
)

const router = useRouter()
const appStore = useAppStore()
const userStore = useUserStore()

const handleJump = (item: itemDataType) => {
    if (!userStore.isLogin && item.isLogin) {
        return appStore.setPopup(popupEnum.LOGIN)
    }

    if (item.link?.path) {
        if (validateUtil.isExternal(item.link?.path)) {
            window.open(item.link?.path, '_blank')
        } else {
            router.push({
                path: item.link?.path,
                query: item.link?.query
            })
        }
    }
}
</script>
